import {Component, OnInit, ViewChild, ViewEncapsulation} from '@angular/core';
import {TrainService} from "app/pages/train/train.service";

import {ActivatedRoute, Params, Router} from "@angular/router";
import {SeedInfiniteScroll} from "app/shared/components/infinite-scroll/seed-infinite-scroll/seed-infinite-scroll.component";
import {TrainStudent} from "../../../../interface";


@Component({
  selector: 'student-list',
  templateUrl: './student-list.component.html',
  styleUrls: ['./student-list.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class StudentListComponent implements OnInit {
  public students: Array<TrainStudent> = [];
  public dataStore = {
    identityType: ''
  };
  public stateStore = {
    haveData: true,
    showRemarkCount: false
  };
  public classInfo = {
    page: 1,
    rows: 10,
    classScheduleId: 0,
    search: null,
    teacherId: 0,
    beginTime: '',
    endTime: '',
  };

  @ViewChild(SeedInfiniteScroll) infiniteScroll;

  constructor(private  trainService: TrainService, private route: ActivatedRoute, private router: Router) {

    this.route.queryParams.subscribe((params: Params) => {
      const classInfo = this.classInfo;
      classInfo.teacherId = params['teacherId'];
      classInfo.classScheduleId = params['classId'];
      classInfo.beginTime = params['beginTime'];
      classInfo.endTime = params['endTime'];
    });
    this.route.params.subscribe((params: Params) => {
      if (params['identityType'] === 'teacher') {
        this.dataStore.identityType = 'teacher';
        this.stateStore.showRemarkCount = true;
      } else {
        this.dataStore.identityType = 'parent';
      }
      this.getStudentListForIdentityType();
    });
  }

  /**
   * @description 根据身份请求相应方法
   * @author ouyqx-691
   * @date 2017-1-5
   */
  getStudentListForIdentityType() {
    if (this.dataStore.identityType === 'teacher') {
      this.getStudentListForTeacher();
    } else {
      this.getStudentListForParent();
    }
  }

  /**
   * @description 根据班级信息查询学员列表（教练）
   * @author ouyqx-691
   * @date 2017-12-31
   */
  getStudentListForTeacher() {
    this.trainService.getStudentListForTeacher(this.classInfo).subscribe((res) => {
      if (res.result === '0' && res.data.list) {
        const students = res.data.list;
        this.classInfo.page++;
        this.infiniteScroll.complete();
        this.students = [...this.students, ...students];
        if (!res.data.hasNextPage) {
          this.infiniteScroll.enable(false);
        }
        if (students.length === 0) {
          return this.stateStore.haveData = false;
        } else {
          return this.stateStore.haveData = true;
        }
      }
    });
  }

  /**
   * @description 根据班级信息查询学员列表（教练）
   * @author ouyqx-691
   * @date 2017-12-31
   */
  getStudentListForParent() {
    this.trainService.getStudentListForParent().subscribe((res) => {
      if (res.result === '0' && res.data) {
        this.students = res.data;
        this.infiniteScroll.complete();
        this.infiniteScroll.enable(false);
      }
    });
  }

  /**
   * @description 实时搜索
   * @author ouyqx-691
   * @date 2017-1-4
   */
  handleKeywordsChange(keywords: string) {
    this.students = [];
    this.classInfo.page = 1;
    this.infiniteScroll.enable(true);
    if (!keywords) {
      this.classInfo.search = null;
    } else {
      this.classInfo.search = keywords;
    }
    this.getStudentListForTeacher();
  }


  /**
   * @description 前往课时列表（教练）
   * @author ouyqx-691
   * @date 2017-12-31
   */
  goToTarget(student) {
    if (this.dataStore.identityType === 'teacher') {
      const params = {
        classId: student.classId,
        studentId: student.studentId,
        courseId: student.courseId,
        name: encodeURI(student.name)
      };
      this.router.navigate([`/train/remark/teacher/lessons`], {queryParams: params}
      );
    } else {
      this.router.navigate(['train/remark/parent/classes'], {queryParams: {studentId: student.id}});
    }
  }

  ngOnInit() {
  }
}



